package com.tj.jinzao.widgets;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Pair;
import android.view.View;

import com.tj.jinzao.common.Constant;

import java.util.List;


/**
 * 首页健康度
 * Created by wjy on 2018/10/11.
 */
public class HealthTripView extends View {

    private Paint mDatePaint;
    private Paint mEventPaint;
    private Paint mEventBgPaint;
    private Paint mLinePaint;
    private Paint mPointOutPaint;
    private Paint mPointInPaint;
    private float mScale;

    private final static int POS[][] = new int[][]{
            {160, 166}, {236, 241}, {238, 358}, {179, 464}, {124, 556}, {113, 681}, {171, 808}, {242, 895}
    };
    //弧线中心点
    private final static int CENTER_POS[][] = new int[][]{
            {220, 190}, {250, 300}, {230, 390}, {140, 500}, {110, 600}, {110, 740}, {220, 850}
    };

    private List<Pair<String, String>> mDataList;

    public HealthTripView(Context context) {
        super(context);
        init();
    }

    public HealthTripView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init() {
        //初始化坐标
        mDatePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mDatePaint.setColor(Color.parseColor("#FF535671"));
        mDatePaint.setTextSize(18 * Constant.SCALE_X);
        mEventPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mEventPaint.setColor(Color.parseColor("#FF71759C"));
        mEventPaint.setTextSize(16 * Constant.SCALE_X);
        mEventBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mEventBgPaint.setColor(Color.parseColor("#FFF0F2F6"));
        mEventBgPaint.setStyle(Paint.Style.FILL);
        mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLinePaint.setColor(Color.parseColor("#FFBDC0DD"));
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setStrokeWidth(4 * Constant.SCALE_X);
        mLinePaint.setPathEffect(new DashPathEffect(new float[]{4 * Constant.SCALE_X, 4 * Constant.SCALE_X}, 0));
        mPointOutPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPointOutPaint.setColor(Color.parseColor("#FFE5E7EF"));
        mPointOutPaint.setStyle(Paint.Style.STROKE);
        mPointOutPaint.setStrokeWidth(1 * Constant.SCALE_X);
        mPointInPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPointInPaint.setColor(Color.parseColor("#FF24D0D0"));
        mPointInPaint.setStyle(Paint.Style.FILL);
        mScale = Constant.SCREEN_WIDTH / 375.0f;
    }

    public void setDataList(List<Pair<String, String>> list) {
        this.mDataList = list;
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = 10;
        if (mDataList != null && mDataList.size() <= POS.length) {
            height = (int) ((POS[mDataList.size() - 1][1] - 100) * mScale);/*+ (int) (500 * mScale)*/
        }
        setMeasuredDimension(Constant.SCREEN_WIDTH, height);
    }

    @Override
    protected void onDraw(Canvas c) {
        if (mDataList != null) {
            int pos = 0;
            Path path = new Path();
            for (Pair<String, String> pair : mDataList) {
                float cx = POS[pos][0] * mScale;
                float cy = (POS[pos][1] - 150) * mScale;
                if (pos == 0) {
                    path.moveTo(cx, cy);
                }
                if (pos + 1 < mDataList.size()) {
                    float dx = POS[pos + 1][0] * mScale;
                    float dy = (POS[pos + 1][1] - 150) * mScale;
                    path.quadTo(CENTER_POS[pos][0] * mScale, (CENTER_POS[pos][1] - 150) * mScale, dx, dy);
                }
                c.drawCircle(cx, cy, 8 * Constant.SCALE_X, mPointInPaint);
                c.drawCircle(cx, cy, 12 * Constant.SCALE_X, mPointOutPaint);
                if (pos % 2 == 0) {
                    mDatePaint.setTextAlign(Paint.Align.RIGHT);
                    mEventPaint.setTextAlign(Paint.Align.RIGHT);
                    c.drawText(pair.first, cx - 15 * Constant.SCALE_X, cy - mDatePaint.getTextSize() / 3, mDatePaint);
                    RectF rectF = new RectF(cx - 45 * Constant.SCALE_X - mEventPaint.measureText(pair.second), cy - mEventPaint.getTextSize() / 5, cx - 15 * Constant.SCALE_X, cy + mEventPaint.getTextSize() * 2);
                    c.drawRoundRect(rectF, rectF.height() / 2, rectF.height() / 2, mEventBgPaint);
                    c.drawText(pair.second, cx - 30 * Constant.SCALE_X, cy + mEventPaint.getTextSize() * 1.3f, mEventPaint);
                } else {
                    mDatePaint.setTextAlign(Paint.Align.LEFT);
                    mEventPaint.setTextAlign(Paint.Align.LEFT);
                    c.drawText(pair.first, cx + 15 * Constant.SCALE_X, cy - mDatePaint.getTextSize() / 3, mDatePaint);
                    RectF rectF = new RectF(cx + 15 * Constant.SCALE_X, cy - mEventPaint.getTextSize() / 5, cx + 45 * Constant.SCALE_X + mEventPaint.measureText(pair.second), cy + mEventPaint.getTextSize() * 2);
                    c.drawRoundRect(rectF, rectF.height() / 2, rectF.height() / 2, mEventBgPaint);
                    c.drawText(pair.second, cx + 30 * Constant.SCALE_X, cy + mEventPaint.getTextSize() * 1.3f, mEventPaint);
                }

                pos++;
            }
            c.drawPath(path, mLinePaint);
        }
    }
}